/**
 * Copyright (c) 2020-2021 浦东大爷 All rights reserved.
 * 机会永远都是留给有准备的人
 */
package com.lagou.service.impl;

import com.lagou.common.Const;
import com.lagou.common.ServerResponse;
import com.lagou.controller.IndustryController;
import com.lagou.mapper.IndustryMapper;
import com.lagou.service.IIndustryService;
import com.lagou.vo.AveragePayVo;
import com.lagou.vo.ConverterVo;
import com.lagou.vo.IndustryVo;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: gitwo
 * @date: 2021-03-10
 * @description:
 */
@Service
@Transactional  //事务控制
public class IndustryServiceImpl implements IIndustryService {

    @Autowired
    private IndustryMapper industryMapper;

    @Override
    public ServerResponse<Map<String, Object>> countCityData() {
        // 查询各个城市招聘情况，创建一个对象封装查询的数据
        List<ConverterVo> converterVoList = industryMapper.countCityPosition();
        // 创建一个map存放总数据
        Map<String, Object> map = new HashMap<>();
        // 创建3个list集合，cityNameList 存放城市名称 thridList 前三名城市数据， dataList 存放十个城市数据
        List<String> cityNameList = new ArrayList<>();
        List<ConverterVo> thridList = new ArrayList<>();
        List<ConverterVo> dataList = new ArrayList<>();
        // 循环list给三个集合封装数据
        for (ConverterVo converterVo : converterVoList) {
            cityNameList.add(converterVo.getName());
            if(thridList.size() < 3){
                thridList.add(converterVo);
            }
            dataList.add(converterVo);
        }
        // 将list存到map中
        map.put("cityNameList", cityNameList);
        map.put("dataList", dataList);
        map.put("thridList", thridList);
        // 将map存放到ServerResponse中
        ServerResponse<Map<String, Object>> successData = ServerResponse.createBySuccessData(map);
        return successData;
    }

    @Override
    public ServerResponse<Map<String, Object>> getIndustryTop() {
        List<IndustryVo> industryVoList = industryMapper.getIndustryTopPosition();
        Map<String, Object> map = new HashMap<>();
        List<String> nameList = new ArrayList<>();
        List<Long> countList = new ArrayList<>();
        for (IndustryVo industryVo : industryVoList) {
            nameList.add(industryVo.getPositionName());
            countList.add(industryVo.getNum());
        }
        map.put("positionList", nameList);
        map.put("countList", countList);

        ServerResponse<Map<String, Object>> response = ServerResponse.createBySuccessData(map);
        return response;
    }

    /**
     * 热门行业对比service实现（销售，服务业，生产制造) 2020-6-4 ~~ 2020-6-9对比
     * @return
     */
    @Override
    public ServerResponse<Map<String, Object>> getIndustryCompare() {

        // new一个HashMap用来存数据
        Map<String, Object> map = new HashMap<>();

        for (String industryName : Const.INDUSTRY) {
            List<Long> countNumList = new ArrayList<>();
            List<IndustryVo> dataList = industryMapper.getIndustryComparePosition(industryName, Const.START_DATE, Const.END_DATE);

            for (IndustryVo industryVo : dataList) {
                countNumList.add(industryVo.getNum());
            }
            if(industryName.equals(Const.INDUSTRY[0])){
                map.put("sell", countNumList);
            }else if(industryName.equals(Const.INDUSTRY[1])){
                map.put("server", countNumList);
            }else if(industryName.equals(Const.INDUSTRY[2])){
                map.put("pro", countNumList);
            }
        }
        map.put("dateTime", Const.DATE_TIME);
        map.put("industryNameList", Const.INDUSTRY);
        return ServerResponse.createBySuccessData(map);
    }

    @Override
    public ServerResponse<Map<String, Object>> getAveragePay() {
        //new一个map存放数据
        Map<String, Object> map = new HashMap<>();
        // 两个list存放城市名和城市平均工资
        List<String> cicyList = new ArrayList<>();
        List<Double> averagePayList = new ArrayList<>();

        List<AveragePayVo> averagePayVoList = industryMapper.getAveragePayPosition();

        for (AveragePayVo averagePayVo : averagePayVoList) {
            cicyList.add(averagePayVo.getCityName());
            averagePayList.add(averagePayVo.getAveragePay());
        }
        map.put("cicyName", cicyList);
        map.put("averagePay", averagePayList);

        return ServerResponse.createBySuccessData(map);
    }
}
